package rule

import (
	"context"
	"errors"
	"reflect"
)

var (
	ruleMap = make(map[string]Rule, 100)
)

type Rule interface {
	Name() string // 返回规则名称

	Check(in *RuleInput) (err error) // 检查是值否符合规则,不符合返回错误
}

type RuleInput struct {
	context.Context // 上下文

	RuleKey string // 规则键

	Value interface{} // 值

	Label string

	Field string

	Kind reflect.Kind
}

func (r *RuleInput) Error() error {
	return errors.New("错误~~~~~")
}

func Register(rule Rule) {
	ruleMap[rule.Name()] = rule
}

func GetRule(name string) (Rule, error) {
	if rule, ok := ruleMap[name]; ok {
		return rule, nil
	}
	return nil, errors.New("rule not found")
}
